package com.qianya.system.manage.web.login.controller;

import cn.hutool.core.util.IdUtil;
import com.qianya.model.common.JWTUtil;
import com.qianya.model.common.ResultCode;
import com.qianya.model.pojo.login.qo.LoginDeptFindQO;
import com.qianya.model.pojo.login.qo.LoginDeptInsertQO;
import com.qianya.system.manage.web.login.model.LoginDept;
import com.qianya.system.manage.web.login.service.ILoginDeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.qianya.model.common.CommonResult;
import com.qianya.model.pojo.login.qo.LoginDeptQO;
import com.qianya.model.pojo.login.vo.LoginDeptVO;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;


/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author wy
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/login-dept")
@Api(value = "LoginDeptController", tags = "部门接口")
@Slf4j
public class LoginDeptController {

    @Autowired
    private ILoginDeptService loginDeptService;

    @PutMapping("/update")
    @ApiOperation("修改部门")
    public CommonResult update(@Valid @RequestBody LoginDeptQO loginDeptQO, HttpServletRequest request) {
        LoginDept loginDept = new LoginDept();
        BeanUtils.copyProperties(loginDeptQO, loginDept);
        return loginDeptService.renewal(loginDept, request);
    }

    @PostMapping("/save")
    @ApiOperation("添加部门")
    public CommonResult save(@Valid @RequestBody LoginDeptInsertQO loginDeptQO, HttpServletRequest request) {

        String uuid = IdUtil.simpleUUID();

        LoginDept loginDept = new LoginDept();

        BeanUtils.copyProperties(loginDeptQO, loginDept);
        if (loginDept.getDeptId() == null) {
            loginDept.setDeptId(uuid);
            loginDept.setCreateId(JWTUtil.getUserId(request.getHeader("token")));
            loginDept.setCreateTime(new Date());
            loginDept.setUpdateTime(null);
        } else {
            loginDept.setUpdateId(JWTUtil.getUserId(request.getHeader("token")));
            loginDept.setUpdateTime(new Date());
        }
        return loginDeptService.create(loginDept);
    }

//    @ApiOperation("查找所有部门(树状结构)")
//    @GetMapping(value = "/find")
//    public CommonResult<IPage<LoginDeptVO>> find(LoginDeptQO loginDeptQO) {
//        LoginDept loginDept = new LoginDept();
//        BeanUtils.copyProperties(loginDeptQO, loginDept);
//        return loginDeptService.find(loginDept);
//    }


    @ApiOperation("查找所有部门")
    @GetMapping(value = "/findAll")
    public CommonResult findAll(@Valid LoginDeptFindQO loginDeptQO) {
        return loginDeptService.findAll(loginDeptQO);
    }


    @ApiOperation("查找所有部门(无条件，不分页)")
    @GetMapping(value = "/findAllDept")
    public CommonResult findAllDept(){
        return loginDeptService.findAllDept();
    }


    @ApiOperation("禁用/启用部门")
    @DeleteMapping(value = "/delete")
    public CommonResult view(@ApiParam(value = "部门id", required = true)
                             @NotBlank(message = "部门id不能为空")
                             @RequestParam(defaultValue = "", name = "deptId") String deptId) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        LoginDept loginDept = loginDeptService.getById(deptId);
        Boolean deptStatus = loginDept.getDeptStatus();
        if (deptStatus) {
            loginDept.setDeptStatus(false);
            loginDept.setUpdateId(JWTUtil.getUserId(request.getHeader("token")));
            loginDept.setUpdateTime(new Date());
            boolean b = loginDeptService.updateById(loginDept);
            if (b) {
                return CommonResult.success("禁用成功");
            }
            return CommonResult.error(ResultCode.ERROR);
        }
        loginDept.setDeptStatus(true);
        loginDept.setUpdateId(JWTUtil.getUserId(request.getHeader("token")));
        loginDept.setUpdateTime(new Date());
        boolean b = loginDeptService.updateById(loginDept);
        if (b) {
            return CommonResult.success("启用成功");
        }
        return CommonResult.error(ResultCode.ERROR);
    }
}
